package tech.popsoft.cip.manage.controller;

import org.springframework.web.bind.annotation.RestController;
import tech.popsoft.cip.manage.service.ApiServicePermissionService;
import tech.popsoft.cip.manage.vo.ApiServicePermissionVO;
import tech.popsoft.platform.common.base.BaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import tech.popsoft.platform.common.annotation.SystemLog;
import tech.popsoft.platform.common.query.QueryGenerator;
import tech.popsoft.platform.common.utils.ResultUtil;
import tech.popsoft.platform.common.vo.PageInfo;
import tech.popsoft.platform.common.vo.Result;
import tech.popsoft.platform.common.vo.SortInfo;
import tech.popsoft.cip.manage.entity.ApiServicePermission;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务权限 前端控制器
 * @author wqliu
 * @date 2021-08-17
 *
 */
@RestController
@RequestMapping("/cip/apiServicePermission")
@Slf4j
public class ApiServicePermissionController extends BaseController {
 @Autowired
 private ApiServicePermissionService apiServicePermissionService;

    //region 基本操作
    /**
     * 初始化
     */
    @ApiOperation(value = "初始化")
    @GetMapping("/init")
    public ResponseEntity<Result> init() {
        ApiServicePermission entity=apiServicePermissionService.init();
        ApiServicePermissionVO vo = convert2VO(entity);
        return ResultUtil.success(vo);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增")
    @PostMapping("/")
    @SystemLog(value = "服务权限-新增")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:add')")
    public ResponseEntity<Result> add(@Validated @RequestBody ApiServicePermissionVO vo) {
        ApiServicePermission entity=convert2Entity(vo);
        apiServicePermissionService.add(entity);
        ApiServicePermissionVO newVO = convert2VO(entity);
        return ResultUtil.success(newVO);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PutMapping("/")
    @SystemLog(value = "服务权限-修改")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:modify')")
    public ResponseEntity<Result> modify(@Validated @RequestBody ApiServicePermissionVO vo)  {
        ApiServicePermission entity=convert2Entity(vo);
        //此处数据库会更新 更新人和更新时间，但数据模型不会刷新
        //个别需展示的该类信息的地方可以重新查询后返回
        apiServicePermissionService.modify(entity);
        ApiServicePermissionVO newVO = convert2VO(entity);
        return ResultUtil.success(newVO);
    }

    /**
     * 删除数据，单条数据标识，或多条数据标识用逗号间隔拼成的字符串
     */
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "标识", required = true, dataType = "String",paramType ="path")
    @DeleteMapping("/{id}")
    @SystemLog(value = "服务权限-删除")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:remove')")
    public ResponseEntity<Result> remove(@PathVariable("id") String id) {
        apiServicePermissionService.remove(id);
        return ResultUtil.success();
    }

    /**
     * 分页
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @SystemLog(value = "服务权限-分页")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:query')")
    public ResponseEntity<Result> page(ApiServicePermissionVO queryVO, PageInfo pageInfo, SortInfo sortInfo) {

        //构造分页对象
        IPage<ApiServicePermission> page = new Page<ApiServicePermission>(pageInfo.getPageNum(), pageInfo.getPageSize());
        //构造查询条件
        QueryWrapper<ApiServicePermission> queryWrapper = QueryGenerator.generateQueryWrapper(ApiServicePermission.class,queryVO,sortInfo);

        //查询数据
        apiServicePermissionService.page(page, queryWrapper);
        //转换vo
        IPage<ApiServicePermissionVO> pageVO = mapperFacade.map(page, IPage.class);
        List<ApiServicePermissionVO>  apiServicePermissionVOList=new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ApiServicePermissionVO vo = convert2VO(page.getRecords().get(i));
            apiServicePermissionVOList.add(vo);
        }
        pageVO.setRecords(apiServicePermissionVOList);        ;
        return ResultUtil.success(pageVO);
    }


    /**
     * 列表
     */
    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    @SystemLog(value = "服务权限-列表")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:query')")
    public ResponseEntity<Result> list(ApiServicePermissionVO queryVO, SortInfo sortInfo) {
        //构造查询条件
        QueryWrapper<ApiServicePermission> queryWrapper = QueryGenerator.generateQueryWrapper(ApiServicePermission.class, queryVO,sortInfo);
        List<ApiServicePermission> list= apiServicePermissionService.list(queryWrapper);
        //转换vo
        List<ApiServicePermissionVO>  apiServicePermissionVOList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ApiServicePermissionVO vo = convert2VO(list.get(i));
            apiServicePermissionVOList.add(vo);
        }
        return ResultUtil.success(apiServicePermissionVOList);
    }

    /**
     * 获取单条数据
     */
    @ApiOperation(value = "获取单条数据")
    @ApiImplicitParam(name = "id", value = "标识", required = true, dataType = "String",paramType ="path")
    @GetMapping("/{id}")
    @SystemLog(value = "服务权限-详情")
    @PreAuthorize("hasPermission(null,'cip:apiServicePermission:view')")
    public ResponseEntity<Result> get(@PathVariable("id") String id) {
        ApiServicePermission entity = apiServicePermissionService.query(id);
        ApiServicePermissionVO vo = convert2VO(entity);
        return ResultUtil.success(vo);
    }

    //endregion

    //region 扩展操作

    //endregion

    //region 辅助操作

    private ApiServicePermissionVO convert2VO(ApiServicePermission entity){
        ApiServicePermissionVO vo=mapperFacade.map(entity,ApiServicePermissionVO.class);
        return vo;
    }

    private ApiServicePermission convert2Entity(ApiServicePermissionVO vo){

        ApiServicePermission entity=mapperFacade.map(vo,ApiServicePermission.class);
        return entity;
    }

   //endregion
}
